package de.unidue.masterproject.plugin;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import de.unidue.masterproject.ccc.exceptions.WebServiceException;
import de.unidue.masterproject.generalinterfaces.IWebServices;
import de.unidue.masterproject.generalmodel.WebserviceType;
import de.unidue.masterproject.generalplugin.GeneralPlugin;
import de.unidue.masterproject.plugin.classes_literaturplugin.Article;
import de.unidue.masterproject.plugin.classes_literaturplugin.ArticleQuote;
import de.unidue.masterproject.plugin.classes_literaturplugin.Book;
import de.unidue.masterproject.plugin.classes_literaturplugin.BookQuote;
import de.unidue.masterproject.plugin.classes_literaturplugin.Literature;
import de.unidue.masterproject.plugin.classes_literaturplugin.Website;
import de.unidue.masterproject.plugin.classes_literaturplugin.WebsiteQuote;

public class FlexibleDialog {
	private JInternalFrame frame, owner;
	private JTreeContainer jTC;
	private JTextField textAuthor, textTitle, textYear, textEdition, textTown,
			textURL, textDate, textMagazine, textPages, textReference;
	private JTextArea textAreaComment, textAreaContent;
	private JButton btnSave;
	private JButton btnPublish;
	private JCheckBox chckbxShared;
	private GeneralPlugin plugin;

	public FlexibleDialog(JInternalFrame frame, JInternalFrame owner,
			JTreeContainer jTC, GeneralPlugin plugin) {
		this.frame = frame;
		this.owner = owner;
		this.jTC = jTC;

		frame.getContentPane().setLayout(null);

		frame.setClosable(false);
		frame.setResizable(false);
		frame.setMaximizable(false);
	}

	private void literatureGUIElements(String title) {
		frame.setBounds(100, 100, 451, 410);
		frame.setTitle(title);

		textAuthor = new JTextField();
		textAuthor.setBounds(202, 32, 207, 20);
		frame.add(textAuthor);
		textAuthor.setColumns(10);

		textTitle = new JTextField();
		textTitle.setColumns(10);
		textTitle.setBounds(202, 63, 207, 20);
		frame.add(textTitle);

		textYear = new JTextField();
		textYear.setColumns(10);
		textYear.setBounds(202, 94, 207, 20);
		frame.add(textYear);

		JLabel lblAuthor = new JLabel("Author:");
		lblAuthor.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblAuthor.setBounds(26, 38, 64, 14);
		frame.add(lblAuthor);

		JLabel lblTitle = new JLabel("Title:");
		lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTitle.setBounds(26, 66, 46, 14);
		frame.add(lblTitle);

		JLabel lblYear = new JLabel("Year:");
		lblYear.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblYear.setBounds(26, 97, 46, 14);
		frame.add(lblYear);

		textAreaComment = new JTextArea();
		textAreaComment.setBounds(26, 215, 386, 97);
		frame.add(textAreaComment);

		JLabel lblComments = new JLabel("Comments:");
		lblComments.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblComments.setBounds(26, 190, 79, 14);
		frame.add(lblComments);

		btnSave = new JButton("Save");
		btnSave.setBounds(26, 323, 164, 38);

		btnPublish = new JButton("Publish");
		btnPublish.setBounds(100, 323, 164, 38);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(245, 323, 164, 38);

		btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnCancel);

		frame.show();
	}

	public void createArticleFormular() {
		literatureGUIElements("Add new article");

		textMagazine = new JTextField();
		textMagazine.setColumns(10);
		textMagazine.setBounds(202, 125, 207, 20);
		frame.add(textMagazine);

		textPages = new JTextField();
		textPages.setColumns(10);
		textPages.setBounds(202, 156, 207, 20);
		frame.add(textPages);

		JLabel lblMagazine = new JLabel("Magazine:");
		lblMagazine.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblMagazine.setBounds(26, 131, 65, 18);
		frame.add(lblMagazine);

		JLabel lblPages = new JLabel("Pages:");
		lblPages.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPages.setBounds(26, 159, 65, 18);
		frame.add(lblPages);

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.createNewArticle(textAuthor.getText(),
						Integer.parseInt(textYear.getText()),
						textTitle.getText(), textAreaComment.getText(),
						textMagazine.getText(), textPages.getText());
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);

	}

	public void createBookFormular() {
		literatureGUIElements("Add new book");

		textEdition = new JTextField();
		textEdition.setColumns(10);
		textEdition.setBounds(202, 125, 207, 20);
		frame.add(textEdition);

		textTown = new JTextField();
		textTown.setColumns(10);
		textTown.setBounds(202, 156, 207, 20);
		frame.add(textTown);

		JLabel lblEdition = new JLabel("Edition:");
		lblEdition.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEdition.setBounds(26, 131, 46, 14);
		frame.add(lblEdition);

		JLabel lblTown = new JLabel("Town:");
		lblTown.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTown.setBounds(26, 159, 46, 14);
		frame.add(lblTown);

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.createNewBook(textAuthor.getText(),
						Integer.parseInt(textYear.getText()),
						textTitle.getText(), textAreaComment.getText(),
						Integer.parseInt(textEdition.getText()),
						textTown.getText());
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		btnPublish.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Book b = new Book(textAuthor.getText(), Integer
						.parseInt(textYear.getText()), textTitle.getText(),
						textAreaComment.getText(), Integer.parseInt(textEdition
								.getText()), textTown.getText());
				try {
					plugin.getWebServices().post(WebserviceType.CUSTOM, b, Book.class, "Testplugin", "", "", "");
				} catch (WebServiceException e1) {
					e1.printStackTrace();
				}
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
		frame.add(btnPublish);

	}

	public void createWebsiteFormular() {
		literatureGUIElements("Add new website");

		textURL = new JTextField();
		textURL.setColumns(10);
		textURL.setBounds(202, 125, 207, 20);
		frame.add(textURL);

		textDate = new JTextField();
		textDate.setColumns(10);
		textDate.setBounds(202, 156, 207, 20);
		frame.add(textDate);

		JLabel lblURL = new JLabel("URL:");
		lblURL.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblURL.setBounds(26, 131, 46, 14);
		frame.add(lblURL);

		JLabel lblDate = new JLabel("Last request:");
		lblDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDate.setBounds(26, 159, 90, 18);
		frame.add(lblDate);

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.createNewWebsite(textAuthor.getText(),
						Integer.parseInt(textYear.getText()),
						textTitle.getText(), textAreaComment.getText(),
						textURL.getText(), textDate.getText());
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void changeArticleFormular(final Article oldArticle) {
		literatureGUIElements("Change existing article");

		textMagazine = new JTextField();
		textMagazine.setColumns(10);
		textMagazine.setBounds(202, 125, 207, 20);
		frame.add(textMagazine);

		textPages = new JTextField();
		textPages.setColumns(10);
		textPages.setBounds(202, 156, 207, 20);
		frame.add(textPages);

		JLabel lblMagazine = new JLabel("Magazine:");
		lblMagazine.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblMagazine.setBounds(26, 131, 65, 18);
		frame.add(lblMagazine);

		JLabel lblPages = new JLabel("Pages:");
		lblPages.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPages.setBounds(26, 159, 65, 18);
		frame.add(lblPages);

		textAuthor.setText(oldArticle.getAuthor());
		textTitle.setText(oldArticle.getTitle());
		textYear.setText(String.valueOf(oldArticle.getYear()));
		textAreaComment.setText(oldArticle.getComment());
		textMagazine.setText(oldArticle.getMagazine());
		textPages.setText(oldArticle.getPages());

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.changeExistingArticle(oldArticle, textAuthor.getText(),
						Integer.parseInt(textYear.getText()),
						textTitle.getText(), textAreaComment.getText(),
						textMagazine.getText(), textPages.getText());

				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void changeBookFormular(final Book oldBook) {
		literatureGUIElements("Change existing book");

		textEdition = new JTextField();
		textEdition.setColumns(10);
		textEdition.setBounds(202, 125, 207, 20);
		frame.add(textEdition);

		textTown = new JTextField();
		textTown.setColumns(10);
		textTown.setBounds(202, 156, 207, 20);
		frame.add(textTown);

		JLabel lblEdition = new JLabel("Edition:");
		lblEdition.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEdition.setBounds(26, 131, 46, 14);
		frame.add(lblEdition);

		JLabel lblTown = new JLabel("Town:");
		lblTown.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTown.setBounds(26, 159, 46, 14);
		frame.add(lblTown);

		textAuthor.setText(oldBook.getAuthor());
		textTitle.setText(oldBook.getTitle());
		textYear.setText(String.valueOf(oldBook.getYear()));
		textAreaComment.setText(oldBook.getComment());
		textEdition.setText(String.valueOf(oldBook.getEdition()));
		textTown.setText(oldBook.getTown());

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// jTC.createNewBook(textAuthor.getText(),
				// Integer.parseInt(textYear.getText()), textTitle.getText(),
				// textAreaComment.getText(),
				// Integer.parseInt(textEdition.getText()), textTown.getText());

				jTC.changeExistingBook(oldBook, textAuthor.getText(),
						Integer.parseInt(textYear.getText()),
						textTitle.getText(), textAreaComment.getText(),
						Integer.parseInt(textEdition.getText()),
						textTown.getText());

				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);

	}

	public void changeWebsiteFormular(final Website oldWebsite) {
		literatureGUIElements("Change existing website");

		textURL = new JTextField();
		textURL.setColumns(10);
		textURL.setBounds(202, 125, 207, 20);
		frame.add(textURL);

		textDate = new JTextField();
		textDate.setColumns(10);
		textDate.setBounds(202, 156, 207, 20);
		frame.add(textDate);

		JLabel lblURL = new JLabel("URL:");
		lblURL.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblURL.setBounds(26, 131, 46, 14);
		frame.add(lblURL);

		JLabel lblDate = new JLabel("Last request:");
		lblDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDate.setBounds(26, 159, 90, 18);
		frame.add(lblDate);

		textAuthor.setText(oldWebsite.getAuthor());
		textTitle.setText(oldWebsite.getTitle());
		textYear.setText(String.valueOf(oldWebsite.getYear()));
		textAreaComment.setText(oldWebsite.getComment());
		textURL.setText(oldWebsite.getUrl());
		textDate.setText(oldWebsite.getDate());

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// jTC.createNewWebsite(textAuthor.getText(),
				// Integer.parseInt(textYear.getText()), textTitle.getText(),
				// textAreaComment.getText(), textURL.getText(),
				// textDate.getText());

				jTC.changeExistingWebsite(oldWebsite, textAuthor.getText(),
						Integer.parseInt(textYear.getText()),
						textTitle.getText(), textAreaComment.getText(),
						textURL.getText(), textDate.getText());

				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	private void quoteGUIElements(String title, Literature literature) {
		frame.setBounds(100, 100, 452, 485);
		frame.setTitle(title);

		JLabel lblReferenceTo = new JLabel("Reference to:");
		lblReferenceTo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblReferenceTo.setBounds(10, 31, 108, 14);
		frame.add(lblReferenceTo);

		textReference = new JTextField();
		textReference.setBounds(146, 24, 280, 28);
		textReference.setEditable(false);
		textReference.setText(literature.toString());
		frame.add(textReference);
		textReference.setColumns(10);

		if (literature instanceof Article || literature instanceof Book) {
			JLabel lblPages = new JLabel("Pages:");
			lblPages.setFont(new Font("Tahoma", Font.PLAIN, 13));
			lblPages.setBounds(10, 80, 108, 14);
			frame.add(lblPages);

			textPages = new JTextField();
			textPages.setColumns(10);
			textPages.setBounds(146, 73, 280, 28);
			frame.add(textPages);
		}

		chckbxShared = new JCheckBox("Share with other users");
		chckbxShared.setFont(new Font("Tahoma", Font.PLAIN, 13));
		chckbxShared.setBounds(146, 121, 157, 23);
		frame.add(chckbxShared);

		textAreaContent = new JTextArea();
		textAreaContent.setBounds(10, 151, 416, 96);
		frame.add(textAreaContent);

		JLabel lblContent = new JLabel("Content:");
		lblContent.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblContent.setBounds(10, 126, 108, 14);
		frame.add(lblContent);

		JLabel lblComment = new JLabel("Comment:");
		lblComment.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblComment.setBounds(10, 258, 108, 14);
		frame.add(lblComment);

		textAreaComment = new JTextArea();
		textAreaComment.setBounds(10, 283, 416, 96);
		frame.add(textAreaComment);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(228, 390, 198, 46);

		btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnCancel);

		btnSave = new JButton("Save");
		btnSave.setBounds(10, 390, 198, 46);

		frame.show();
	}

	public void createArticleQuoteFormular(final Article article,
			final String ownerName) {
		quoteGUIElements("Add new article quote", article);

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.createNewArticleQuote(article, textAreaContent.getText(),
						textAreaComment.getText(), ownerName,
						chckbxShared.isSelected(), textPages.getText());
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void createBookQuoteFormular(final Book book, final String ownerName) {
		quoteGUIElements("Add new book quote", book);

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.createNewBookQuote(book, textAreaContent.getText(),
						textAreaComment.getText(), ownerName,
						chckbxShared.isSelected(), textPages.getText());
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void createWebsiteQuoteFormular(final Website website,
			final String ownerName) {
		quoteGUIElements("Add new website quote", website);

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				jTC.createNewWebsiteQuote(website, textAreaContent.getText(),
						textAreaComment.getText(), ownerName,
						chckbxShared.isSelected());
				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void changeArticleQuoteFormular(final ArticleQuote oldAQ,
			final String ownerName) {
		quoteGUIElements("Change existing article quote", oldAQ.getReference());

		textReference.setText(oldAQ.getReference().toString());
		textPages.setText(oldAQ.getPages());
		chckbxShared.setSelected(oldAQ.isPublished());
		textAreaContent.setText(oldAQ.getContent());
		textAreaComment.setText(oldAQ.getComment());

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// jTC.createNewArticleQuote(article,textAreaContent.getText(),textAreaComment.getText(),ownerName,chckbxShared.isSelected(),textPages.getText());

				jTC.changeExistingArticleQuote(oldAQ,
						textAreaContent.getText(), textAreaComment.getText(),
						ownerName, chckbxShared.isSelected(),
						textPages.getText());

				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void changeBookQuoteFormular(final BookQuote oldBQ,
			final String ownerName) {
		quoteGUIElements("Change existing book quote", oldBQ.getReference());

		textReference.setText(oldBQ.getReference().toString());
		textPages.setText(oldBQ.getPages());
		chckbxShared.setSelected(oldBQ.isPublished());
		textAreaContent.setText(oldBQ.getContent());
		textAreaComment.setText(oldBQ.getComment());

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// jTC.createNewBookQuote(book,textAreaContent.getText(),textAreaComment.getText(),ownerName,chckbxShared.isSelected(),textPages.getText());

				jTC.changeExistingBookQuote(oldBQ, textAreaContent.getText(),
						textAreaComment.getText(), ownerName,
						chckbxShared.isSelected(), textPages.getText());

				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public void changeWebsiteQuoteFormular(final WebsiteQuote oldWQ,
			final String ownerName) {
		quoteGUIElements("Change existing website quote", oldWQ.getReference());

		textReference.setText(oldWQ.getReference().toString());
		chckbxShared.setSelected(oldWQ.isPublished());
		textAreaContent.setText(oldWQ.getContent());
		textAreaComment.setText(oldWQ.getComment());

		btnSave.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// jTC.createNewWebsiteQuote(website,textAreaContent.getText(),textAreaComment.getText(),ownerName,chckbxShared.isSelected());

				jTC.changeExistingWebsiteQuote(oldWQ,
						textAreaContent.getText(), textAreaComment.getText(),
						ownerName, chckbxShared.isSelected());

				owner.setVisible(true);
				frame.getContentPane().removeAll();
				frame.hide();
			}
		});

		frame.add(btnSave);
	}

	public GeneralPlugin getPlugin() {
		return plugin;
	}

}
